﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GM10采集器数据解析
{
    public partial class GM10 : Form
    {
        public GM10()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true; // 确定可以打开多个文件
            ofd.Title = "请选择文件";
            ofd.Filter = "txt格式（*.txt）|*.txt|所有文件|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string file = ofd.FileName;
                TextBox.AppendText(file + "\n"); // 显示路径
                TextBox.AppendText("KL-W6000 返回数据: " + "\n");
                System.IO.StreamReader streamReader = new System.IO.StreamReader(file, System.Text.Encoding.Default);
                String data = streamReader.ReadToEnd().TrimStart();
                String[] substrings = data.Split('\n');
                String time = substrings[1]; // 第一行时间
                int spaceIndex = time.IndexOf(' ');
                if (spaceIndex > 0)
                {
                    time = "日期时间" + time.Substring(spaceIndex, time.Length - spaceIndex - 1) + ", ";
                    spaceIndex = substrings[2].IndexOf(" ");
                    if (spaceIndex > 0)
                    {
                        time += substrings[2].Substring(spaceIndex);
                    }
                    else
                    {
                        MessageBox.Show("数据非法（不是GM10返回数据）");
                    }
                }
                else
                {
                    MessageBox.Show("数据非法（不是GM10返回数据）");
                }

                String output = time;
                for (int i = 3; i < substrings.Length - 1; i++)
                {
                    String str = substrings[i];
                    spaceIndex = str.IndexOf("C");
                    if (spaceIndex != -1)
                    {
                        char state = str[0];
                        String channel = str.Substring(1, spaceIndex - 1).Replace(" ", "");
                        int chan = int.Parse(channel); // 信道编号
                        string numStr = str.Substring(spaceIndex + 1, str.Length - spaceIndex - 2).Replace(" ", "");
                        double temperature = double.Parse(numStr); // 温度
                        output += "通道" + chan + "  : ";
                        switch (state)
                        {
                            case 'N':
                                output += " " + temperature + " °C \n";
                                break;
                            case 'D':
                                output += " 插值输入";
                                break;
                            case 'S':
                                output += " 不使用";
                                break;
                            case 'O':
                                output += " 溢出";
                                break;
                            case 'E':
                                output += " 错误";
                                break;
                            case 'B':
                                output += " 断偶";
                                break;
                        }
                    }
                    else 
                    {
                        output += " 数据格式有误\n";
                    }
                }
                TextBox.AppendText(output + "\n");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
